//
//  MoreServiceView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/10.
//

import UIKit

class ManagerView: UIView {
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    init() {
        super.init(frame: CGRect.zero)
        backgroundColor = .white
        layer.cornerRadius = 5
        setMenus(menus: [ProfileMenuModel.init(title: "消息通知", imageName: "notice", className: "NoticeCenterViewController"),ProfileMenuModel.init(title: "收藏", imageName: "store",className: "StoreViewController"),ProfileMenuModel.init(title: "浏览历史", imageName: "history",className: "StoreViewController"),ProfileMenuModel.init(title: "下载管理", imageName: "download",className: "DwonloadViewController")])
    }
    var delegate:MenuServiceDelegate?
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setMenus(menus:Array<ProfileMenuModel>) {
        var temp:UIView?
        for n in 0..<menus.count {
            let m = menus[n]
            let v = MenuView.init(m.title, imageName: m.imageName,isRed:true){[self]() in
                self.delegate?.itemSelected(className: m.className,title: m.title)
            }
            addSubview(v)
            
            v.snp.makeConstraints({
                if(temp != nil){
                    if n%4 == 0 {
                        $0.top.equalTo((temp?.snp.bottom)!)
                        $0.left.equalTo(0)
                    }else{
                        $0.left.equalTo((temp?.snp.right)!)
                        $0.top.equalTo(temp!)
                    }
                }else{
                    $0.left.top.equalTo(0)
                }
                $0.width.equalToSuperview().dividedBy(4)
                if(n == menus.count - 1){
                    $0.bottom.equalTo(0)
                }
            })
            temp = v
        }
    }
    
}
public protocol MenuServiceDelegate{
    func itemSelected(className:String,title:String)
}
